package com.wanghongli.week2fitment.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wanghongli.week2fitment.entity.Fitment;
import com.wanghongli.week2fitment.service.HouseService;
import com.wanghongli.week2fitment.service.IFitmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author whl
 * @since 2021-04-28
 */
@RestController
@RequestMapping("/fitment")
public class FitmentController {

    @Autowired
    private IFitmentService service;


    @Autowired
    private HouseService houseService;

    @RequestMapping("/list")
    public IPage<Fitment> list(@RequestParam(defaultValue = "1")int pageNum,@RequestParam(defaultValue = "3")int pageSize,Fitment fitment){
        QueryWrapper queryWrapper=new QueryWrapper();
        if (fitment.getHnum()!=null&& fitment.getHnum()!=""){
            Integer getbyhnum = houseService.getbyhnum(fitment.getHnum());
            if (getbyhnum!=null){
                queryWrapper.eq("hid",getbyhnum);
            }
        }
        if (fitment.getUser()!=null && fitment.getUser()!=""){
            queryWrapper.eq("user",fitment.getUser());
        }
        if (fitment.getUserphone()!=null && fitment.getUserphone()!=""){
            queryWrapper.eq("userphone",fitment.getUserphone());
        }

        Page<Fitment> page = service.page(new Page<>(pageNum, pageSize),queryWrapper);
        for (Fitment f:page.getRecords()) {
            f.setHnum(houseService.getbyid(f.getHid()).getHnum());
        }
        return page;
    }

    @RequestMapping("save")
    public  Boolean save(@RequestBody Fitment fitment){
        Integer getbyhnum = houseService.getbyhnum(fitment.getHnum());
        fitment.setHid(getbyhnum);
        return  service.save(fitment);
    }

    @RequestMapping("update")
    public  Boolean update(@RequestBody Fitment fitment){
        Integer getbyhnum = houseService.getbyhnum(fitment.getHnum());
        fitment.setHid(getbyhnum);
        return  service.updateById(fitment);
    }

}

